package com.kinglone.back.role.service.impl;

import com.kinglone.back.permission.dao.PermissionMapper;
import com.kinglone.back.permission.model.Permission;
import com.kinglone.back.role.dao.RoleMapper;
import com.kinglone.back.role.model.Role;
import com.kinglone.back.role.service.RoleService;
import com.kinglone.common.node.ZTreeNode;
import com.kinglone.common.utils.ResultMsg;
import com.kinglone.common.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	@Resource
	private RoleMapper roleMapper;
	@Resource
	private PermissionMapper permissionMapper;


    @Override
    public List<Role> list(Map<String, Object> map) {
        return roleMapper.list(map);
    }

	@Override
	public int getTotal(Map<String, Object> map) {
		return roleMapper.getTotal(map);
	}

    @Override
    public List<ZTreeNode> roleTreeList() {
        return roleMapper.roleTreeList();
    }

    @Override
    public List<ZTreeNode> roleTreeListByRoleId(String[] roleId) {
        return roleMapper.roleTreeListByRoleId(roleId);
    }

    @Override
    @Transactional
    public ResultMsg setAuthority(Integer roleId, String ids) {
        ResultMsg msg = new ResultMsg();
        // 删除该角色所有的权限
        roleMapper.deleteRolesById(roleId);
      //  ShiroUtils.clearNowUserAuth(); //清除缓存的权限
        // 添加新的权限
        for (String id : ids.split(",")) {
            Permission permission = new Permission();
            permission.setId(UUIDUtils.getUUID());
            permission.setRoleid(String.valueOf(roleId));
            permission.setMenuid(id);
            permissionMapper.insert(permission);
        }
        msg.setMessage("权限分配成功!");
        return msg;
    }

    @Override
    public String getRoleNameByRoleId(Integer roleId) {
        return roleMapper.getRoleNameByRoleId(roleId);
    }


}
